#ifndef __SINGLETON_H__
#define __SINGLETON_H__

namespace Nezha
{

	template <typename T>
	class Singleton
	{
	public:
		static T& getInstance(void)
		{
			static T t;
			return t;
		}

	protected:
		Singleton() {}
	private:
		Singleton(const Singleton&);
		Singleton&	operator = (const Singleton &s);
	};


	template <typename T>
	class SingletonEx
	{
	public:
		SingletonEx( void )
		{
			assert( !msSingleton );
			msSingleton = static_cast< T* >( this );
		}
		~SingletonEx( void )
		{
			assert( msSingleton );
			msSingleton = 0;
		}

		//static T& getSingleton( void )
		//{
		//	assert(msSingleton);
		//	return (*msSingleton);
		//}
		//static T* getSingletonPtr( void )
		//{
		//	return msSingleton;
		//}
	protected:
		static T* msSingleton;
	private:
		SingletonEx(const SingletonEx<T> &) {}
		SingletonEx& operator=(const SingletonEx<T> &) { return *this; }
	};


}//end namespace Nezha

#define NEZHA_SINGLETON_DECLARE(classname)\
	public:\
	static classname& getSingleton();\
	static classname* getSingletonPtr();

#define NEZHA_SINGLETON_IMPLEMENT(classname)\
	template<> classname* SingletonEx<classname>::msSingleton = 0;\
	classname& classname::getSingleton(){ return *msSingleton; }\
	classname* classname::getSingletonPtr() { return msSingleton; }

#endif //end __SINGLETON_H__